package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/user")
@Api(tags = "测试")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping(value = "/abc")
    public String getName1(){
        return  "ling";
    }

    //获取请求头中的数据
    @GetMapping("select")
    public  String select(@RequestHeader("token") String tokenParm){
        return "token: " + tokenParm;
    }

    //使用@PathVariable：接收请求路径中占位符的值
    @GetMapping("{id}")
    public String select1(@PathVariable("id") Integer id){
        return "传入的值为：" + id;
    }

    //不使用任何注解
    @GetMapping("user")
    public  String user(String name){
        return "姓名：" + name;
    }

    //对象方式接收参数,前端可以传递任何User对象中对应的字段，后端都可以接收到
    @GetMapping("all")
    public  String select2(User user){
        return user.toString();
    }


    @PostMapping("/abcd")
    public List<User> getUser(){
        return userService.findAll();
    }

    @PostMapping("/abcde")
    public List<User> getAge(){
        return userService.findAge();
    }

    @PostMapping("/pack")
    public  void packed( HttpServletResponse response, HttpServletRequest request){
        userService.packJson(response);
    }

}
